<template>

  <!-- andt实现抽屉 -->
  <div style="height:100%;">
    <div v-if="!showRight" style="display: flex;justify-content: center;align-items: center; height:100%; ">
      <img style="width: 107px; height: 128px;cursor: pointer" @click="showRight=true" src="/imgs/展开按钮切图拷贝.png"
           alt="">
    </div>
    <div style=" height:100%;" v-if="showRight">
      <div style="display: flex;align-items: center; height:100%;float: left ">
        <img style="width: 107px; height: 128px;cursor: pointer" @click="showRight=false" src="/imgs/收缩按钮切图.png"
             alt="">
      </div>
      <div style="float: left;width:342px;height:976px;    margin-left: -40px;">
        <div style="background-image: url('/imgs/右侧背景切图.png');width:100%;height:100%">
          <div style="padding:20px 20px 20px 40px">
            <a-input v-model:value="searchValue"
                     style="margin-bottom: 8px; border-radius: 30px;width: 200px" placeholder="搜索"/>
            <a-tree
                :expanded-keys="expandedKeys"
                :auto-expand-parent="autoExpandParent"
                @expand="onExpand"
                :tree-data="gData"
                checkable
                :height="750"
            >
              <template #title="{ key: treeKey, title,level }">
                <a-dropdown :trigger="['contextmenu']">
          <span v-if="title.indexOf(searchValue) > -1">
          <div v-if="level==1" class="right-btn1" @click="btn1Click(title)">{{ title }}</div>
               <div v-if="level==2" class="right-btn2" @click="btn2Click(title)">{{ title }}</div>
               <div v-if="level==3" class="right-btn3" @click="btn3Click(title)">{{ title }}</div>
               <div v-if="level==4" class="right-btn4" @click="btn4Click(title)">{{ title }}</div>
          </span>

                  <template #overlay>
                    <a-menu @click="({ key: menuKey }) => onContextMenuClick(treeKey, menuKey)">
                      <a-menu-item key="create">创建</a-menu-item>
                      <a-menu-item key="rename">重命名</a-menu-item>
                      <a-menu-item key="del">删除</a-menu-item>
                    </a-menu>
                  </template>
                </a-dropdown>
              </template>
            </a-tree>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>
<script lang="ts">
//1.先使用import导入你要在该组件中使用的子组件
import {defineComponent, ref} from 'vue';
import NewRightTree from "./NewRightTree.vue";


import {defineComponent, ref, UnwrapRef, watch} from 'vue';
import type {TreeProps} from 'ant-design-vue';
import { message } from 'ant-design-vue';
import { useStore } from 'vuex';
const genData: TreeProps['treeData'] = [];
const generateList = (data: TreeProps['treeData']) => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({key, title: key});
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(genData);

const dataList: TreeProps['treeData'] = [];
const getParentKey = (
    key: string | number,
    tree: UnwrapRef<TreeProps["treeData"]>,
): string | number | undefined => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};

export default defineComponent({
  components: {

  },
  setup() {
    const visible = ref(false);
    const showRight = ref(false)
    const afterVisibleChange = (bool: boolean) => {
      console.log('visible', bool);
    };

    const showDrawer = () => {
      visible.value = true;
    };

    const onClose = () => {
      visible.value = false;
    };

    const expandedKeys = ref<(string | number)[]>([]);
    const searchValue = ref<string>('');
    const autoExpandParent = ref<boolean>(true);
    const store = useStore();
    const treeData = [

      {
        title: "模型",
        key: "模型",
        level: 1,
        children: [
          {title: "大唐不夜城", key: "大唐不夜城", level: 2},
          {title: "大唐宾馆", key: "大唐宾馆", level: 2},
        ],
      },
      {
        title: "驻地",
        key: "驻地",
        level: 1,
        children: [
          {title: "倾斜模型", key: "倾斜模型", level: 2},
          {title: "三区划分", key: "三区划分", level: 2},
          {title: "警力部署", key: "警力部署", level: 2},
          {title: "无人机反制", key: "无人机反制", level: 2},
          {
            title: "重点部位",
            key: "重点部位", level: 2,

            children: [
              {
                title: "加油站", key: "加油站", level: 3,
                children: [
                  {
                    title: "加油站_1",
                    key: "加油站_1",
                    level: 4
                  }

                ]
              },
              {title: "加气站", key: "加气站", level: 3},
              {title: "应急避险", key: "应急避险", level: 3},
            ],
          },
          {title: "任务流线", key: "任务流线", level: 2},
        ],
      },
      {
        title: "现场",
        key: "现场",
        level: 1
      },
      {
        title: "路线",
        key: "路线",
        level: 1
      },
    ]
    const gData = ref<TreeProps['treeData']>(treeData);
    console.log("newTreeData" + gData.toString());
    const onExpand = (keys: string[]) => {
      expandedKeys.value = keys;
      autoExpandParent.value = false;
    };

    const onContextMenuClick = (treeKey, menuKey) => {
      message.info(treeKey+menuKey);
      if(menuKey == 'create'){
        store.commit('createModel',treeKey)
      }

    };
    const btn2Click= (title) => {
      console.log(title)
      store.commit('CHANGE_COUNT',title)
    };
    watch(searchValue, value => {
      const expanded = dataList
          .map((item: TreeProps['treeData'][number]) => {
            if (item.title.indexOf(value) > -1) {
              return getParentKey(item.key, gData.value);
            }
            return null;
          })
          .filter((item, i, self) => item && self.indexOf(item) === i);
      expandedKeys.value = expanded;
      searchValue.value = value;
      autoExpandParent.value = true;
    });

    return {
      visible,
      afterVisibleChange,
      showDrawer,
      onClose,
      showRight,
      expandedKeys,
      searchValue,
      autoExpandParent,
      gData,
      onExpand,
      onContextMenuClick,
      btn2Click
    };
  },
});
</script>

<style lang="less">
.rightContent {
  background: url("/imgs/right-bg.png");
}
.ant-tree {
  background: transparent;
  color: #fff
}

.right-btn1 {
  width: 149px;
  height: 69px;
  background: url("/imgs/一级按钮未选中切图.png") no-repeat 100%;
  text-align: center;
  line-height: 70px;

  cursor: pointer;
}

.right-btn1:hover {
  background: url("/imgs/一级按钮选中切图.png") no-repeat 100%;
}

.right-btn1_h {
  width: 149px;
  height: 69px;
  background: url("/imgs/一级按钮选中切图.png") no-repeat 100%;
  text-align: center;
  line-height: 70px;

  cursor: pointer;
}

.right-btn2 {
  width: 110px;
  height: 42px;
  background: url("/imgs/二级标题未选中切图.png");
  text-align: center;
  line-height: 36px;
  margin: 0px 0px 0px;
  cursor: pointer;
}

.right-btn2:hover {
  background: url("/imgs/二级按钮选中切图.png");
}

.right-btn2_h {
  width: 110px;
  height: 42px;
  background: url("/imgs/二级按钮选中切图.png");
  text-align: center;
  line-height: 36px;
  margin: 0px 30px 10px;
  cursor: pointer;
}

.right-btn3 {
  width: 101px;
  height: 32px;
  background: url("/imgs/三级标题未选中按钮切图.png");
  text-align: center;
  line-height: 32px;
  margin: 0px 0px 0px;

  cursor: pointer;
}

.right-btn3:hover {
  background: url("/imgs/三级标题选中切图.png");
}

.right-btn4 {
  width: 101px;
  height: 32px;
  background: url("/imgs/三级标题未选中按钮切图.png");
  text-align: center;
  line-height: 32px;
  margin: 0px 0px 0px;

  cursor: pointer;
}

.right-btn4:hover {
  background: url("/imgs/三级标题选中切图.png");
}

.right-btn3_h {
  width: 101px;
  height: 32px;
  background: url("/imgs/三级标题选中切图.png");
  text-align: center;
  line-height: 32px;
  margin: 0px 60px 10px;
  cursor: pointer;
}

.ant-tree .ant-tree-node-content-wrapper.ant-tree-node-selected {
  background-color: transparent;
}

.ant-tree .ant-tree-node-content-wrapper:hover {
  background-color: transparent;
}

.a-menu .ant-dropdown ant-dropdown-placement-rightTop {
  background-color: transparent;
}

.ant-dropdown-menu {
  background-color: transparent;
}

.ant-dropdown-menu-title-content {
  color: #ffffff;
}

.ant-dropdown-menu :hover{
  background-color: transparent;
}
</style>